/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nutriknowmobileapp;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Felipe
 */
public class HelloMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command calcularCommand;
    private Command backCommand;
    private Command itemCommand;
    private Form form;
    private TextField pesoTextField;
    private ChoiceGroup sexoChoiceGroup;
    private TextField alturaTextField;
    private TextField idadeTextField;
    private Form form1;
    private StringItem idealStringItem;
    private StringItem gastoStringItem;
    private StringItem situacaoStringItem;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display;
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|24-preAction
            if (command == calcularCommand) {//GEN-END:|7-commandAction|1|24-preAction
               double peso = Double.parseDouble(pesoTextField.getString());
                double altura = Double.parseDouble(alturaTextField.getString());
                int idade = Integer.parseInt(idadeTextField.getString());
                double ideal;
                double gasto;                           
                
                switchDisplayable(null, getForm1());//GEN-LINE:|7-commandAction|2|24-postAction
                
          try{         
                    
                    if(sexoChoiceGroup.isSelected(0)){
                        ideal = (altura-100) - ((altura-150)/4);
                        gasto = 66.5 + (13.7*peso) + (5*altura) - (6.8*idade);
                    }else{
                        ideal = (altura-100) - ((altura-150)/2);
                        gasto = 665 + (9.6*peso) + (1.7*altura) - (4.7*idade);
                    }
               
                idealStringItem.setText(Double.toString(ideal)+" Kg");
                gastoStringItem.setText(Double.toString(gasto)+" Kcal");
                if(ideal < peso){
                    situacaoStringItem.setText("Você está acima do seu peso ideal!");
                }else if (ideal > peso){
                    situacaoStringItem.setText("Você está abaixo do seu peso ideal!");
                }else{
                    situacaoStringItem.setText("Você está no seu peso ideal!");
                }
            }catch(Exception e){
                    Alert erro = new Alert("Erro");
                    erro.setString("Erro. Por favor tente novamente");
                    erro.setTimeout(4000);
                    getDisplay().setCurrent(erro);
            }
                
                
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|19-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|26-preAction
        } else if (displayable == form1) {
            if (command == backCommand) {//GEN-END:|7-commandAction|5|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm());//GEN-LINE:|7-commandAction|6|26-postAction
                pesoTextField.setString(null);
                alturaTextField.setString(null);
                idadeTextField.setString(null);
                sexoChoiceGroup.setSelectedIndex(0, true);
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
//</editor-fold>//GEN-END:|7-commandAction|8|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("C\u00E1lculo de Peso Ideal e Gasto Energ\u00E9tico", new Item[]{getPesoTextField(), getAlturaTextField(), getIdadeTextField(), getSexoChoiceGroup()});//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getCalcularCommand());
            form.addCommand(getExitCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|14-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: calcularCommand ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initialized instance of calcularCommand component.
     *
     * @return the initialized component instance
     */
    public Command getCalcularCommand() {
        if (calcularCommand == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            calcularCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return calcularCommand;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|25-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form1 ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of form1 component.
     *
     * @return the initialized component instance
     */
    public Form getForm1() {
        if (form1 == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            form1 = new Form("Seus Dados", new Item[]{getIdealStringItem(), getGastoStringItem(), getSituacaoStringItem()});//GEN-BEGIN:|22-getter|1|22-postInit
            form1.addCommand(getBackCommand());
            form1.setCommandListener(this);//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return form1;
    }
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: pesoTextField ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initialized instance of pesoTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getPesoTextField() {
        if (pesoTextField == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            pesoTextField = new TextField("Peso em kilogramas:", "", 32, TextField.ANY);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return pesoTextField;
    }
//</editor-fold>//GEN-END:|29-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: alturaTextField ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initialized instance of alturaTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getAlturaTextField() {
        if (alturaTextField == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            alturaTextField = new TextField("Altura em cent\u00EDmetros:", "", 32, TextField.ANY);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return alturaTextField;
    }
//</editor-fold>//GEN-END:|30-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: sexoChoiceGroup ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initialized instance of sexoChoiceGroup component.
     *
     * @return the initialized component instance
     */
    public ChoiceGroup getSexoChoiceGroup() {
        if (sexoChoiceGroup == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            sexoChoiceGroup = new ChoiceGroup("Sexo:", Choice.EXCLUSIVE);//GEN-BEGIN:|31-getter|1|31-postInit
            sexoChoiceGroup.append("Masculino", null);
            sexoChoiceGroup.append("Feminino", null);
            sexoChoiceGroup.setSelectedFlags(new boolean[]{false, false});//GEN-END:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return sexoChoiceGroup;
    }
//</editor-fold>//GEN-END:|31-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: idealStringItem ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initialized instance of idealStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getIdealStringItem() {
        if (idealStringItem == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            idealStringItem = new StringItem("Peso Ideal:", "");//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return idealStringItem;
    }
//</editor-fold>//GEN-END:|35-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: gastoStringItem ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initialized instance of gastoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getGastoStringItem() {
        if (gastoStringItem == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            gastoStringItem = new StringItem("Gasto Energ\u00E9tico:", "");//GEN-LINE:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return gastoStringItem;
    }
//</editor-fold>//GEN-END:|36-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initialized instance of itemCommand component.
     *
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return itemCommand;
    }
//</editor-fold>//GEN-END:|37-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: idadeTextField ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initialized instance of idadeTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getIdadeTextField() {
        if (idadeTextField == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            idadeTextField = new TextField("Idade:", "", 32, TextField.ANY);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return idadeTextField;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: situacaoStringItem ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initialized instance of situacaoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getSituacaoStringItem() {
        if (situacaoStringItem == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            situacaoStringItem = new StringItem("Situa\u00E7\u00E3o:", "");//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return situacaoStringItem;
    }
//</editor-fold>//GEN-END:|40-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
